package com.gxa.insurance.controller;


import com.gxa.insurance.pojo.*;
import com.gxa.insurance.service.*;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ClassName ExportController
 * @Description TODO
 * @Author ds
 * @Date 2022/6/1 11:10
 * @Version 1.0
 */
@Controller
@RequestMapping("/export")
@CrossOrigin
public class ExportController {

    @Autowired
    CustomerService customerService = null;

    @Autowired
    CardService cardService;

    @Autowired
    EtcApplySevice etcApplySevice;

    @Autowired
    S4ShopService s4ShopService;

    @Autowired
    ExpressRepairShopService expressRepairShopService;

    @Autowired
    RepairDepotService repairDepotService;

    @Autowired
    OrderService orderService;

    @GetMapping("/order")
    public void exportOrder(HttpServletResponse response) throws Exception {

        List<Order> orders = orderService.selectAllOrderList();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("订单列表");
        String fileName = "订单列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"订单编号", "订单类型", "姓名", "城市", "订单来源", "手机号", "车牌号", "接车司机", "是否还车", "还车司机", "订单状态", "下单时间", "付款状态"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }
        System.out.println("---" + orders);
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        for (Order item : orders) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(item.getNumber());
            if(item.getOrderType().getOrderType() == null){
                dataRow.createCell(1).setCellValue("");
            }else{
                dataRow.createCell(1).setCellValue(item.getOrderType().getOrderType());
            }
            if(item.getName() == null){
                dataRow.createCell(2).setCellValue("");
            }else{
                dataRow.createCell(2).setCellValue(item.getName());
            }
            if(item.getCity().getCity() == null){
                dataRow.createCell(3).setCellValue("");
            }else{
                dataRow.createCell(3).setCellValue(item.getCity().getCity());
            }
            if(item.getOrderSource().getOrderSource() == null){
                dataRow.createCell(4).setCellValue("");
            }else{
                dataRow.createCell(4).setCellValue(item.getOrderSource().getOrderSource());
            }
            if(item.getPhone() == null){
                dataRow.createCell(5).setCellValue("");
            }else{
                dataRow.createCell(5).setCellValue(item.getPhone());
            }
            if(item.getCarId() == null){
                dataRow.createCell(6).setCellValue("");
            }else{
                dataRow.createCell(6).setCellValue(item.getCarId());
            }
            if(item.getDiver() == null){
                dataRow.createCell(7).setCellValue("");
            }else{
                dataRow.createCell(7).setCellValue(item.getDiver());
            }
            if(item.getIsReturn() == null){
                dataRow.createCell(8).setCellValue("");
            }else{
                dataRow.createCell(8).setCellValue(item.getIsReturn());
            }
            if(item.getReturnDriver() == null){
                dataRow.createCell(9).setCellValue("");
            }else{
                dataRow.createCell(9).setCellValue(item.getReturnDriver());
            }
            if(item.getStatus().getOrderStatus() == null){
                dataRow.createCell(10).setCellValue("");
            }else{
                dataRow.createCell(10).setCellValue(item.getStatus().getOrderStatus());
            }
            if(dateFormat.format(item.getCreateTime()) == null){
                dataRow.createCell(11).setCellValue("");
            }else{
                dataRow.createCell(11).setCellValue(dateFormat.format(item.getCreateTime()));
            }
            if(item.getPay().getStatus() == null){
                dataRow.createCell(12).setCellValue("");
            }else{
                dataRow.createCell(12).setCellValue(item.getPay().getStatus());
            }
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }


    @GetMapping("/repairDepot")
    public void exportRepairDepot(HttpServletResponse response) throws Exception {

        List<RepairDepot> repairDepots = repairDepotService.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("修理厂列表");
        String fileName = "修理厂列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "店铺名称", "短名称", "店铺主修车型", "店铺折扣", "区域", "地址", "是否签约"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }


        // 添加数据行:
        int rowNum = 1;
        for (RepairDepot item : repairDepots) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getShortName());
            dataRow.createCell(3).setCellValue(item.getMajorModel());
            dataRow.createCell(4).setCellValue(item.getDiscount());
            dataRow.createCell(5).setCellValue(item.getArea());
            dataRow.createCell(6).setCellValue(item.getAddress());
            dataRow.createCell(7).setCellValue(item.getIsSigned());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }


    @GetMapping("/expressRepairShop")
    public void exportExpressRepairShop(HttpServletResponse response) throws Exception {

        List<ExpressRepairShop> expressRepairShops = expressRepairShopService.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("快修店列表");
        String fileName = "快修店列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "店铺名称", "短名称", "店铺主修车型", "店铺折扣", "区域", "地址", "是否签约"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }


        // 添加数据行:
        int rowNum = 1;
        for (ExpressRepairShop item : expressRepairShops) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getShortName());
            dataRow.createCell(3).setCellValue(item.getMajorModel());
            dataRow.createCell(4).setCellValue(item.getDiscount());
            dataRow.createCell(5).setCellValue(item.getArea());
            dataRow.createCell(6).setCellValue(item.getAddress());
            dataRow.createCell(7).setCellValue(item.getIsSigned());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

    @GetMapping("/s4Shop")
    public void exportS4Shop(HttpServletResponse response) throws Exception {

        List<S4Shop> s4Shops = s4ShopService.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("4s店列表");
        String fileName = "4s店列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "店铺名称", "短名称", "店铺主修车型", "店铺折扣", "区域", "地址", "是否签约"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }


        // 添加数据行:
        int rowNum = 1;
        for (S4Shop item : s4Shops) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getShortName());
            dataRow.createCell(3).setCellValue(item.getMajorModel());
            dataRow.createCell(4).setCellValue(item.getDiscount());
            dataRow.createCell(5).setCellValue(item.getArea());
            dataRow.createCell(6).setCellValue(item.getAddress());
            dataRow.createCell(7).setCellValue(item.getIsSigned());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

    @GetMapping("/customer")
    public void exportCustomer(HttpServletResponse response) throws Exception {

        List<Customer> customers = customerService.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("客户列表");
        String fileName = "客户列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "联系人", "手机号", "登录时间"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        // 添加数据行:
        int rowNum = 1;
        for (Customer item : customers) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getContacts());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(dateFormat.format(item.getLoginTime()));
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

    @GetMapping("/card")
    public void exportCard(HttpServletResponse response) throws Exception {

        List<Card> cards = cardService.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("车主卡列表");
        String fileName = "车主卡列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "姓名", "手机号", "车牌号", "车辆时间", "车辆初登日期", "交强险到期日", "申请时间"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        // 添加数据行:
        int rowNum = 1;
        for (Card item : cards) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getPhone());
            dataRow.createCell(2).setCellValue(item.getCarId());
            dataRow.createCell(3).setCellValue(item.getBrand());
            dataRow.createCell(4).setCellValue(dateFormat.format(item.getBeginTime()));
            dataRow.createCell(5).setCellValue(dateFormat.format(item.getCompulsoryEndTime()));
            dataRow.createCell(6).setCellValue(dateFormat.format(item.getCommercialEndTime()));
            dataRow.createCell(7).setCellValue(dateFormat.format(item.getRegTime()));

            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

    }

    @GetMapping("/etcApply")
    public void etcexp(HttpServletResponse response) throws Exception {

        List<EtcApply> etcApplies = etcApplySevice.selectAll();

        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("etc列表");
        String fileName = "etc列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"编号", "名字", "手机号", "车牌号", "是否是京籍", "地址", "申请时间", "银行名称"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);   // 在行中添加单元格
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);   // 把字段名添加到表头行上
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        // 添加数据行:
        int rowNum = 1;
        for (EtcApply item : etcApplies) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getPhone());
            dataRow.createCell(3).setCellValue(item.getLcNumber());
            dataRow.createCell(4).setCellValue(item.getCitizen());
            dataRow.createCell(5).setCellValue(item.getAddress());
            dataRow.createCell(6).setCellValue(dateFormat.format(item.getTime()));
            dataRow.createCell(7).setCellValue(item.getBank());

            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
